package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.EditingAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.PostedEditing00Bean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 08 月 27 日
 * 作者  Z2043 welbeing to hangzou com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 *
 * @desc 编辑页面
 */
public class EditingActivity extends Activity {

    private static final String TAG ="EditingActivity" ;
    @Bind(R.id.activity_editing_iv_return)
    ImageView activityEditingIvReturn;
    @Bind(R.id.activity_editing_tv_titile)
    TextView activityEditingTvTitile;
    @Bind(R.id.activity_editing_et_titile)
    EditText activityEditingEtTitile;
    @Bind(R.id.activity_editing_tv_content)
    TextView activityEditingTvContent;
    @Bind(R.id.activity_editing_et_content)
    EditText activityEditingEtContent;
    @Bind(R.id.activity_editing_iv_add_editing)
    ImageView activityEditingIvAddEditing;
  /*  @Bind(R.id.activity_editing_rl_posted)
    RelativeLayout activityEditingRlPosted;*/
  @Bind(R.id.activity_editing_tv_posted)
  TextView activityEditingTvPosted;

    @Bind(R.id.activity_editing_recyclerview)
    RecyclerView activityEditingRecyclerView;
    private  int i = 1;


    private Bitmap head;//头像Bitmap
    private static String path = "/sdcard/DemoHead/";//sd路径
    private String confirmNickname;
    private EditingAdapter editingAdapter;
    private List<Bitmap> mlist = new ArrayList();
    private static List<File>  mfileList = new ArrayList();
    private Set<File>  mset = new HashSet();



    private String circle_id;
    private String circle_num;
    private String circle_name;
    private String circle_state;
    private String circle_url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editing);
        ButterKnife.bind(this);


        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(true, this);

        Intent intent = getIntent();
        circle_id = intent.getStringExtra("circle_id");
        circle_name = intent.getStringExtra("circle_name");
        circle_num = getIntent().getStringExtra("circle_num");
        circle_name = getIntent().getStringExtra("circle_name");
        circle_state = getIntent().getStringExtra("circle_state");
        circle_url = getIntent().getStringExtra("circle_url");


        activityEditingEtTitile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                activityEditingTvTitile.setText("");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });



        activityEditingEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                activityEditingTvContent.setText("");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });


        GridLayoutManager layoutManager = new GridLayoutManager(this,2);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityEditingRecyclerView.setLayoutManager(layoutManager);
        editingAdapter = new EditingAdapter();
        activityEditingRecyclerView.setAdapter(editingAdapter);

    }
    /**
     * 监听Back键按下事件,方法1:
     * 注意:
     * super.onBackPressed()会自动调用finish()方法,关闭
     * 当前Activity.
     * 若要屏蔽Back键盘,注释该行代码即可
     */
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
        intent.putExtra("circle_id",circle_id);
        intent.putExtra("circle_name",circle_name);
        intent.putExtra("circle_num",circle_num);
        intent.putExtra("circle_state",circle_state);
        intent.putExtra("circle_url",circle_url);
        startActivity(intent);
        finish();
    }
    @OnClick({R.id.activity_editing_iv_return, R.id.activity_editing_iv_add_editing, R.id.activity_editing_tv_posted})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_editing_iv_return:
                Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                intent.putExtra("circle_id",circle_id);
                intent.putExtra("circle_name",circle_name);
                intent.putExtra("circle_num",circle_num);
                intent.putExtra("circle_state",circle_state);
                intent.putExtra("circle_url",circle_url);
                startActivity(intent);
                finish();
                break;
            case R.id.activity_editing_iv_add_editing:
                LlHeadPortrait();
                break;
            case R.id.activity_editing_tv_posted:
                mfileList.addAll(mset);
                String trimTitile = activityEditingEtTitile.getText().toString().trim();
                String trimContent = activityEditingEtContent.getText().toString().toString().trim();
                if(!TextUtils.isEmpty(trimTitile)){
                    if(!TextUtils.isEmpty(trimContent)){
                        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
                        String user_id = sp.getString("user_id", "");
                        for (File file : mfileList) {
                            Log.e(TAG,"activity_editing_tv_posted-----mfileList12474---------->"+file);
                        }
                        if(!user_id.equals("")){
                            Log.e(TAG,"activity_editing_tv_posted--mfileList------------->"+mfileList.size());
                            if(mfileList.size() == 0){
                                postedEditing_00(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 1){
                                postedEditing_01(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 2){
                                postedEditing_02(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 3){
                                postedEditing_03(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 4){
                                postedEditing_04(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 5){
                                postedEditing_05(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 6){
                                postedEditing_06(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 7){
                                postedEditing_07(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 8){
                                postedEditing_08(trimTitile,trimContent,user_id);
                            }else if(mfileList.size() == 9){
                                postedEditing_09(trimTitile,trimContent,user_id);
                            }else{
                                Toast.makeText(EditingActivity.this,"图片过多",Toast.LENGTH_SHORT).show();
                            }
                        }else{
                            Toast.makeText(EditingActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                        }
                    }else{
                        Toast.makeText(EditingActivity.this,"请填写正文",Toast.LENGTH_SHORT).show();
                    }
                }else{
                    Toast.makeText(EditingActivity.this,"请填写标题",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private void postedEditing_09(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .addFile("cus_logo_04", "head_04.jpg", mfileList.get(3))//
                .addFile("cus_logo_05", "head_05.jpg", mfileList.get(4))//
                .addFile("cus_logo_06", "head_06.jpg", mfileList.get(5))//
                .addFile("cus_logo_07", "head_07.jpg", mfileList.get(6))//
                .addFile("cus_logo_08", "head_08.jpg", mfileList.get(7))//
                .addFile("cus_logo_09", "head_09.jpg", mfileList.get(8))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse-------9------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_08(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .addFile("cus_logo_04", "head_04.jpg", mfileList.get(3))//
                .addFile("cus_logo_05", "head_05.jpg", mfileList.get(4))//
                .addFile("cus_logo_06", "head_06.jpg", mfileList.get(5))//
                .addFile("cus_logo_07", "head_07.jpg", mfileList.get(6))//
                .addFile("cus_logo_08", "head_08.jpg", mfileList.get(7))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----8---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_07(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .addFile("cus_logo_04", "head_04.jpg", mfileList.get(3))//
                .addFile("cus_logo_05", "head_05.jpg", mfileList.get(4))//
                .addFile("cus_logo_06", "head_06.jpg", mfileList.get(5))//
                .addFile("cus_logo_07", "head_07.jpg", mfileList.get(6))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----7---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_06(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .addFile("cus_logo_04", "head_04.jpg", mfileList.get(3))//
                .addFile("cus_logo_05", "head_05.jpg", mfileList.get(4))//
                .addFile("cus_logo_06", "head_06.jpg", mfileList.get(5))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----6---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    private void postedEditing_05(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .addFile("cus_logo_04", "head_04.jpg", mfileList.get(3))//
                .addFile("cus_logo_05", "head_05.jpg", mfileList.get(4))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----5---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    private void postedEditing_04(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .addFile("cus_logo_04", "head_04.jpg", mfileList.get(3))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----4---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_03(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
                .addFile("cus_logo_03", "head_03.jpg", mfileList.get(2))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----3---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_02(String trimTitile, String trimContent, String user_id) {
        Log.e(TAG,"postedEditing_02---mfileList.get(0)----------->"+mfileList.get(0));
        Log.e(TAG,"postedEditing_02-----mfileList.get(1)--------->"+mfileList.get(1)); //.addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))//
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .addFile("cus_logo_02", "head_02.jpg", mfileList.get(1))
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----2---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                    Log.e(TAG,"postedEditing_02-------------->");
                                }
                            });
                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_01(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils.post()//
                .addFile("cus_logo_01", "head_01.jpg", mfileList.get(0))//
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()//
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        //gson.fromJson(stringparseNetworkResponse,)
                        Log.e(TAG,"stringparseNetworkResponse----1---------->"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });

                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    private void postedEditing_00(String trimTitile, String trimContent, String user_id) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.POSTTING)
                .addParams("po_name",trimTitile )
                .addParams("cat_name",circle_name)
                .addParams("content",trimContent)
                .addParams("cat_id",circle_id)
                .addParams("cus_id",user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"stringparseNetworkResponse===========>"+stringparseNetworkResponse);
                        final PostedEditing00Bean postedEditing00Bean = gson.fromJson(stringparseNetworkResponse, PostedEditing00Bean.class);
                        if(postedEditing00Bean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,"发布成功",Toast.LENGTH_SHORT).show();
                                    Intent intent = new Intent(EditingActivity.this, TrueCircleActivity.class);
                                    intent.putExtra("circle_id",circle_id);
                                    intent.putExtra("circle_name",circle_name);
                                    intent.putExtra("circle_num",circle_num);
                                    intent.putExtra("circle_state",circle_state);
                                    intent.putExtra("circle_url",circle_url);
                                    startActivity(intent);
                                    finish();
                                }
                            });

                        }else {
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(EditingActivity.this,postedEditing00Bean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    /**
     * @desc 弹出选项框供用户选择
     * @desc 选项框有从相册中选择 拍照  取消 上传头像
     * */
    private void LlHeadPortrait() {
       /* new AlertView("上传头像", null, "取消", null, new String[]{"拍照", "从相册中选择"}, this, AlertView.Style.ActionSheet, new OnItemClickListener(){
            public void onItemClick(Object o,int position){
                // Toast.makeText(PersonalInformationActivity.this, "点击了第" + position + "》》》》》》个", Toast.LENGTH_SHORT).show();
                *//**
                 * @desc 拍照是 0
                 * @desc 从相册中选择 1
                 * @desc 取消 -1
                 * *//*
                if(position == -1){  //取消 -1

                }else if(position == 0){  //拍照是 0
                    //最好用try/catch包裹一下，防止因为用户未给应用程序开启相机权限，而使程序崩溃
                    try {
                        Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//开启相机应用程序获取并返回图片（capture：俘获）
                        intent2.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                "head.jpg")));//指明存储图片或视频的地址URI
                        startActivityForResult(intent2, 2);//采用ForResult打开
                    } catch (Exception e) {
                        Toast.makeText(EditingActivity.this, "相机无法启动，请先开启相机权限", Toast.LENGTH_LONG).show();
                    }
                }else if(position == 1){  //从相册中选择 1
                    Intent intent6 = new Intent(Intent.ACTION_PICK, null);//返回被选中项的URI
                    intent6.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image*//*");//得到所有图片的URI
                    startActivityForResult(intent6, 1);
                }

            }
        }).show();*/
        Intent intent6 = new Intent(Intent.ACTION_PICK, null);//返回被选中项的URI
        intent6.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");//得到所有图片的URI
        startActivityForResult(intent6, 1);
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            //从相册里面取相片的返回结果
            case 1:
                if (resultCode == RESULT_OK) {
                    cropPhoto(data.getData());//裁剪图片
                }
                break;
            //相机拍照后的返回结果
            case 2:
                if (resultCode == RESULT_OK) {
                    File temp = new File(Environment.getExternalStorageDirectory() + "/head.jpg");
                    cropPhoto(Uri.fromFile(temp));//裁剪图片
                    Log.e(TAG,"setPicToView------123244-----Uri.fromFile(temp)--->"+Uri.fromFile(temp));
                    Log.e(TAG,"setPicToView------123244----temp---->"+temp);
                }
                break;
            //调用系统裁剪图片后
            case 3:
                if (data != null) {
                    head = ((Bitmap)data.getExtras().getParcelable("data"));
                    if (head != null) {
                        /**
                         * 上传服务器代码
                         */
                        setPicToView(head);//保存在SD卡中
                       // activityEditingIvAddEditing.setImageBitmap(BitmapcCrcular.makeRoundCorner(head));//用ImageView显示出来
                        //activityEditingIvAddEditing.setImageBitmap(head);
                        if(mlist.size()<9){
                            editingAdapter.addItem(head);
                            mlist.add(head);
                            Log.e(TAG,"setPicToView------123244-----head--->"+head);
                        }else{
                            Toast.makeText(EditingActivity.this,"发布的图片不能超过九张",Toast.LENGTH_SHORT).show();
                        }

                    }
                }
                break;
            case 4:
                if (data != null) {
                    confirmNickname = data.getStringExtra("confirmNickname");
                    if(confirmNickname == null){
                        confirmNickname = "麻麻";
                    }
                    //tv_01.setText(confirmNickname);
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 调用系统的裁剪
     *
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        //找到指定URI对应的资源图片
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
      /*  intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);*/
        intent.putExtra("return-data", true);
        //进入系统裁剪图片的界面
        startActivityForResult(intent, 3);
    }

    private void setPicToView(Bitmap mBitmap) {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd卡是否可用
            return;
        }
        FileOutputStream b = null;
        File file = new File(path);
        file.mkdirs();// 创建以此File对象为名（path）的文件夹
        String fileName = path + "head";//图片名字
        try {

            if(mfileList.size() < 9){
                b = new FileOutputStream(path + "head_0"+i+".jpg");
                mset.add(new File(path + "head_0"+i+".jpg"));
            }
            i= i +1;
            Log.e(TAG,"v-------------->"+b);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件（compress：压缩） path+"head_0"+i+".jpg")

           // SubmiAvatar(new File(fileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流
                if(b != null){
                    b.flush();
                    b.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    protected void onPause() {
        super.onPause();
       // mfileList.clear();
        mlist.clear();
    }
}
